package com.yue.ssa.controller;

import com.yue.ssa.bean.RespBean;
import com.yue.ssa.bean.User;
import com.yue.ssa.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Api(value = "用户管理", tags = "用户管理接口")
@RestController
@RequestMapping("rest/user")
public class UserController {
    @Autowired
    UserService userService;

    @PreAuthorize("hasAuthority('system_user::read')")
    @ApiOperation(value = "根据用户id获取用户信息", notes = "根据用户id获取用户信息")
    @ApiImplicitParam(name = "id", value = "用户主键", required = true, dataType = "String")
    @GetMapping("/{id}")
    public RespBean get(@PathVariable("id") String id) {
        return userService.get(id);
    }

    @ApiOperation(value = "判断用户名是否存在", notes = "判断用户名是否存在")
    @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String")
    @GetMapping("/usernameIsExist")
    public boolean usernameIsExist(String username) {
        return userService.usernameIsExist(username);
    }

    @PreAuthorize("hasAuthority('system_user::write')")
    @ApiOperation(value = "新增用户", notes = "新增用户")
    @PostMapping
    public RespBean add(@RequestBody User user) {
        return userService.add(user);
    }

    @PreAuthorize("hasAuthority('system_user::write')")
    @ApiOperation(value = "修改用户", notes = "修改用户")
    @PutMapping
    public RespBean update(@RequestBody User user) {
        return userService.update(user);
    }

    @PreAuthorize("hasAuthority('system_user::read')")
    @ApiOperation(value = "用户信息分页", notes = "用户信息分页")
    @ApiImplicitParams({@ApiImplicitParam(value = "关键字", name = "keyword", paramType = "query"),
            @ApiImplicitParam(value = "页码", name = "pageNo", paramType = "query"),
            @ApiImplicitParam(value = "条数", name = "pageSize", paramType = "query")})
    @GetMapping("page")
    public RespBean page(String keyword, @RequestParam(defaultValue = "1") Integer pageNo, @RequestParam(defaultValue = "10") Integer pageSize) {
        return userService.page(keyword, pageNo, pageSize);
    }

    @ApiOperation(value = "获取当前用户信息", notes = "获取当前用户信息")
    @GetMapping("current")
    public RespBean current() {
        return userService.current();
    }

    @PreAuthorize("hasAuthority('system_user::write')")
    @ApiOperation(value = "删除用户", notes = "删除用户")
    @ApiImplicitParam(name = "ids", value = "用户主键", required = true, allowMultiple = true, dataType = "String")
    @DeleteMapping("/delete")
    public RespBean delete(@RequestParam String[] ids) {
        return userService.delete(ids);
    }

    @ApiOperation(value = "修改密码", notes = "修改密码")
    @ApiImplicitParams({@ApiImplicitParam(required = true, value = "原始密码", name = "oldPassword", paramType = "query"),
            @ApiImplicitParam(required = true, value = "新密码", name = "newPassword", paramType = "query")})
    @GetMapping("changePassword")
    public RespBean changePassword(@RequestParam String oldPassword, @RequestParam String newPassword) {
        return userService.changePassword(oldPassword, newPassword);
    }

    @PreAuthorize("hasAuthority('system_user::write')")
    @ApiOperation(value = "重置密码", notes = "重置密码")
    @ApiImplicitParam(name = "id", value = "用户主键", required = true, dataType = "String")
    @GetMapping("/resetPassword")
    public RespBean resetPassword(String id) {
        return userService.resetPassword(id);
    }
}
